package tt.dz.appserver.service.business.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.dao.ext.InfomationDao;
import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.model.pub.Infomation;
import tt.dz.appserver.entity.vo.InfomationVo;
import tt.dz.appserver.service.business.InformationService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.date.DateUtil;
import tt.dz.appserver.util.enums.Response;

/**
 * 资讯管理实现类
 * @author USER001
 *
 */

public class InformationServiceImpl implements InformationService {

	@Resource
	InfomationDao infomationDao; 
	
	@Override
	public JSONObject pubinformationadd(String title, String intro, String url, 
			String imageurl, String remark) throws AvroRemoteException {
		// TODO Auto-generated method stub
		Infomation information = new Infomation();
		JSONObject resultJsonObject = new JSONObject();
		String createtime = DateUtil.getDateTime();
		Integer validflag = 1;
		
		String infoid = "000000" + DateUtil.getDateTimeLong();
				
		information.setInfo_id(infoid);
		information.setTitle(title);
		information.setIntro(intro);
		information.setUrl(url);
		information.setImage_url(imageurl);
		information.setCreate_time(DateUtil.parseStringToDateTime(createtime));
		information.setRemark(remark);
		information.setValid_flag(validflag);
		
		infomationDao.addInfomation(information);
		
		resultJsonObject.put("infoid", infoid);
		resultJsonObject.put("title", title);
		resultJsonObject.put("intro", intro);
		resultJsonObject.put("url", url);
		resultJsonObject.put("imageurl", imageurl);
		resultJsonObject.put("createtime", createtime);
		resultJsonObject.put("remark", remark);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public JSONObject pubinformationdel(String infoid) throws AvroRemoteException {
		// TODO Auto-generated method stub
		Infomation information = new Infomation();
		JSONObject resultJsonObject = new JSONObject();
		Integer validflag = 0;		
		
		information.setInfo_id(infoid);
		information.setValid_flag(validflag);
		
		infomationDao.delInfomation(information);
		
		resultJsonObject.put("infoid", infoid);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public JSONObject pubinformationupdate(String infoid, String title, String intro, String url, 
			String imageurl, String remark) throws AvroRemoteException {
		// TODO Auto-generated method stub
		Infomation information = new Infomation();
		JSONObject resultJsonObject = new JSONObject();
		Integer validflag = 1;		
		
		information.setInfo_id(infoid);
		information.setTitle(title);
		information.setIntro(intro);
		information.setUrl(url);
		information.setImage_url(imageurl);
		information.setRemark(remark);
		information.setValid_flag(validflag);
		
		infomationDao.updateInfomation(information);
		
		resultJsonObject.put("infoid", infoid);
		resultJsonObject.put("title", title);
		resultJsonObject.put("intro", intro);
		resultJsonObject.put("url", url);
		resultJsonObject.put("imageurl", imageurl);
		resultJsonObject.put("remark", remark);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public List<Infomation> pubinformationselect() throws AvroRemoteException {
		
		return infomationDao.findInfomationValid();
		
	}

	@Override
	public ResultMappingEntity getInfomationList(int pageNo) {

		ResultMappingEntity mappingEntity = null;
		List<Infomation> infomationsList = infomationDao.findInfomationList(pageNo);
		
		List<InfomationVo> infomationVos = new ArrayList<>(); 
		
		for (Infomation infomation : infomationsList) {
			InfomationVo infomationVo = new InfomationVo();
			infomationVo.setCreateTime(DateUtil.getDateTime(infomation.getCreate_time()));
			infomationVo.setImageUrl(infomation.getImage_url());
			infomationVo.setInfoId(infomation.getInfo_id());
			infomationVo.setIntro(infomation.getIntro());
			infomationVo.setRemark(infomation.getRemark());
			infomationVo.setTitle(infomation.getTitle());
			infomationVo.setUrl(infomation.getUrl());
			infomationVos.add(infomationVo);
		}
		
		mappingEntity = Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), infomationVos);
		return mappingEntity;
	}
}
